function Ar = imagerestore(as)
% imagerestore    Restore an image from approximation made by imageapprox
%
%  Not well tested yet.
%
% Ar = imagerestore(res);
%----------------------------------------------------------------------
%  Ar       The restored image
%  res      A struct with the data needed to restore image
%           this should be the results from imageapprox. The fields are
%  .tr      The transform to use, 'none', 'dct', 'lot', 'elt', 'm79', ...
%  .sizes   Size of image, adjusted image, transform and X (not W)
%  .del     and 'thr', 'deldc', and 'thrdc', bin sizes used in quantizer
%  .Ds      A struct with the dictionary (if used)
%  .ecm     Entropy coding method
%  .y       The byte sequence
%           if ecm=='' or ecm=='none', y==[], then these fields should be
%  .Zdc     Quantized DC component (1xL)
%  .Zw      Quantized W (weight matrix)
%----------------------------------------------------------------------
% example:
%  A = double(imread('lena.bmp')) - 128; peak = 255;
%  res = imageapprox(A, 't','m79', 's',8, 'tPSNR',37, 'del',-18, 'v',1);
%  res.Ar = []; res.W = [];
%  Ar = imagerestore(res);  % restore from Zw and Zdc
%  R = A - Ar; PSNR = 10*log10(((numel(A)*peak^2)/sum(sum(R.*R)))); 
%  res = imageapprox(A, 't','m79', 's',8, 'tPSNR',37, 'del',-18, 'v',1, 'eb','Huff06');
%  res.Ar = []; res.W = []; res.Zdc = []; res.Zw = [];
%  Ar = imagerestore(res);  % restore from byte sequence y
%
%  par = struct('d','ex312Jan191930.mat', 'tPSNR',37, 'del',-18, 'v',1, 'qLimit',[0.49,0.51], 'eb','Huff06');
%  res = imageapprox(A, par );
%  res.Ar = []; res.W = []; res.Zdc = []; res.Zw = [];
%  Ar = imagerestore(res);  % restore from byte sequence y

%----------------------------------------------------------------------
% Copyright (c) 2009.  Karl Skretting.  All rights reserved.
% University of Stavanger (Stavanger University), Signal Processing Group
% Mail:  karl.skretting@uis.no   Homepage:  http://www.ux.his.no/~karlsk/
% 
% HISTORY:  dd.mm.yyyy
% Ver. 1.0  11.12.2009  Made function, isfield does not work
% Ver. 1.1  01.03.2010  Updated since imageapprox is updated
% Ver. 1.2  01.06.2011  Some errors corrected
%----------------------------------------------------------------------

if ~((nargin == 1) && isstruct(as))
    error('imagerestore: not a struct as first and only input argument.');
end

N = as.sizes.N;
L = as.sizes.L;
if isfield(as,'ecm') && isfield(as,'y') && (numel(as.y) > 0)
    if strcmpi(as.ecm,'Huff06');
        disp('imagerestore: Try to do inverse Huffman coding.');
        try
            xC = Huff06(as.y);
        catch ME
            Ar = ME;
            return
        end
    end
    if strcmpi(as.ecm,'Arith06');
        disp('imagerestore: Try to do inverse Arithmetic coding (Arith06).');
        try
            xC = Arith06(as.y);
        catch ME
            Ar = ME;
            return
        end
    end
    if strcmpi(as.ecm,'Arith07');
        disp('imagerestore: Try to do inverse Arithmetic coding (Arith07).');
        try
            xC = Arith07(as.y);
        catch ME
            Ar = ME;
            return
        end
    end
    if exist('xC','var')
        if isfield(as,'Ds') && isstruct(as.Ds)
            % a dictionary was used, first 4 sequences are Zw
            xCw = cell(4,1);
            xCdc = cell(numel(xC)-4,1);
            for i=1:numel(xCw); xCw{i}=xC{i}; end;
            for j=1:numel(xCdc); xCdc{j}=xC{4+j}; end;
            % disp(xCw);
            % disp(xCdc{1});
            Zw = myreshape(xCw);
            Zdc = mypred(xCdc);       % FEIL HER 
        else  % a transform was used (no dictionary)
            if numel(xC) == 7;
                Zw = myreshape(xC);
                Zdc = Zw(1,:);
                Zw(1,:) = 0;
            elseif numel(xC) == 8;
                Zdc = xC{8};
                for i=2:numel(Zdc); Zdc(i) = Zdc(i)+Zdc(i-1); end;
                temp = cell(7,1);
                for i=1:7; temp{i} = xC{i}; end;
                Zw = myreshape(temp);
                clear temp
            else
                disp('imagerestore: wrong size of xC.');
            end
        end
    end
else
    Zdc = as.Zdc;
    Zw = as.Zw;
end
    
if ~isfield(as, 'del')
    Ar = 'imagerestore: quantizing step del is missing.';
    disp();
    return;
end
if ~isfield(as, 'thr'); as.thr = as.del; end;
if ~isfield(as, 'deldc'); as.deldc = as.del; end;
if ~isfield(as, 'thrdc'); as.thrdc = as.deldc/2; end;
    
Qdc = uniquant(Zdc, as.deldc, as.thrdc);
Qw = uniquant(Zw, as.del, as.thr);

if strcmpi(as.tr,'none')  % find Xr from Qdc
    Xr = ones(N,1)*(Qdc/sqrt(N));
else
    Xr = [Qdc(:)'; zeros(N-1,L)];
end
%
if isfield(as,'Ds') && isstruct(as.Ds)    % sparse W, Xa = Ds.D * W,
    Ar = mycol2im( Xr + as.Ds.D*Qw, 't', as.tr, ...
        'imsize', [as.sizes.Maa, as.sizes.Naa], ...
        's', [as.sizes.Ms, as.sizes.Ns]);
else
    Ar = mycol2im( Xr + Qw, 't', as.tr, ...
        'imsize', [as.sizes.Maa, as.sizes.Naa], ...
        's', [as.sizes.Ms, as.sizes.Ns]);
end
if (as.sizes.Maa ~= as.sizes.Ma) || (as.sizes.Naa ~= as.sizes.Na)
    Ar = Ar(1:as.sizes.Ma, 1:as.sizes.Na);
end

return

